//
//  FormBasedController.m
//  sl
//
//  Created by Yang Zhang on 4/15/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "SLFormBasedController.h"


@implementation SLFormBasedController
@synthesize theScrollView = _theScrollView;
@synthesize theContentView = _theContentView;
@synthesize activeView = _activeView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)releaseOutlets{
	[super releaseOutlets];
	
	self.theContentView = nil;
	self.theScrollView = nil;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
	[self.theScrollView addSubview:self.theContentView];
	self.theScrollView.contentSize = self.theContentView.bounds.size;
}

- (void)viewWillAppear:(BOOL)animated{
	[super viewWillAppear:animated];
	
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(keyboardDidShow:)
												 name: UIKeyboardDidShowNotification
											   object:nil];
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(keyboardDidHide:)
												 name: UIKeyboardDidHideNotification
											   object:nil];
}

- (void)viewDidAppear:(BOOL)animated{
	[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated{
	[super viewWillDisappear:animated];
	[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
	[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}

- (void)viewDidDisappear:(BOOL)animated{
	[super viewDidDisappear:animated];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark - Keyboard

- (void) keyboardDidShow: (NSNotification *)notif {
	if (keyboardVisible) {
		return;
	}
	
	NSDictionary* info = [notif userInfo];
	NSValue* aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
	CGSize keyboardSize = [aValue CGRectValue].size;
	
	float kh = keyboardSize.height - 49; // TODO: change this
	
	originalScrollViewFrame = self.theScrollView.frame;
	originalScrollViewOffset = self.theScrollView.contentOffset;
	
	CGRect scrollFrame = self.theScrollView.frame;
	scrollFrame.size.height -= kh;
	self.theScrollView.frame = scrollFrame;
	
	CGRect viewFrame = [self.activeView frame];
	viewFrame.origin.y += 10;
	[self.theScrollView scrollRectToVisible:viewFrame animated:YES];
	
	keyboardVisible = YES;
	
}

- (void) keyboardDidHide: (NSNotification *)notif {
	if (!keyboardVisible) {
		return;
	}
	
	NSDictionary* info = [notif userInfo];
	float dur = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
	
	[UIView animateWithDuration:dur animations:^{
		self.theScrollView.frame = originalScrollViewFrame;
		self.theScrollView.contentOffset = originalScrollViewOffset;
		
	}completion:^(BOOL finished){
		
	}];
	
	keyboardVisible = NO;
	
}


#pragma mark -

- (BOOL) textFieldShouldBeginEditing:(UITextField*)textField {
	self.activeView = textField;
	int tag = textField.tag;
	if(tag > 0){
		UIView *next = [textField.superview viewWithTag:(tag + 1)];
		if(next){
			textField.returnKeyType = UIReturnKeyNext;
		}else{
			textField.returnKeyType = UIReturnKeyGo;
		}
	}
	return YES;
}

- (BOOL) textFieldShouldReturn:(UITextField *)textField{
	int tag = textField.tag;
	if(tag > 0){
		UIView *next = [textField.superview viewWithTag:(tag + 1)];
		if(next){
			[next becomeFirstResponder];
		}else{
			// last has reached
			if([self validateForm]){
				[textField resignFirstResponder];
				self.activeView = nil;
				[self submitForm];
			}
		}
	}
	return YES;
}


#pragma mark - To overwrite


- (BOOL)validateForm{
	return YES;
}

- (void)submitForm{
	
}


- (IBAction)submitAction:(id)sender{
	if([self validateForm]){
		[self.activeView resignFirstResponder];
		self.activeView = nil;
		
		[self submitForm];
	}
}



@end
